import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'login.dart';

class RegisterPage extends StatefulWidget {
  const RegisterPage({Key? key}) : super(key: key);

  @override
  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.blueGrey,
        title: Text(
          "注册",
        ),
        centerTitle: true,
        elevation: 5.0,
        // 阴影
      ),
      body: FromofLogin(),
    );
  }
}

class FromofLogin extends StatefulWidget {
  const FromofLogin({Key? key}) : super(key: key);

  @override
  _FromofLoginState createState() => _FromofLoginState();
}

class _FromofLoginState extends State<FromofLogin> {
  GlobalKey _globalKey = GlobalKey<FormState>();
  TextEditingController _textEditingController01 = TextEditingController();
  TextEditingController _textEditingController02 = TextEditingController();
  TextEditingController _textEditingController03 = TextEditingController();
  FocusNode _username_focus = FocusNode();
  FocusNode _password_focus = FocusNode();
  FocusNode _parepeat_focus = FocusNode();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _textEditingController01.dispose();
    _textEditingController02.dispose();
    _textEditingController03.dispose();
    _username_focus.dispose();
    _password_focus.dispose();
    _parepeat_focus.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
        padding: EdgeInsets.fromLTRB(40, 0, 40, 0),
        child: Form(
          key: _globalKey,
          child: Column(
            children: [
              SizedBox(height: 50),
              TextFormField(
                inputFormatters: [
                  WhitelistingTextInputFormatter(RegExp("[a-zA-Z|0-9]|_"),),
                ],
                autofocus: true,
                focusNode: _username_focus,
                controller: _textEditingController01,
                decoration: InputDecoration(
                  prefixIcon: Icon(Icons.person),
                  labelText: "用户名(仅允许输入字母、数字以及下划线)",
                  hintText: "username",
                ),
                textInputAction: TextInputAction.next,
                validator: (v){
                  RegExp reg = new RegExp(r'^\d{11}$');
                  if (!reg.hasMatch(v!)) {
                    return '请输入11位手机号码';
                  }
                  return null;
                },
                onFieldSubmitted: (v) {},
              ),
              SizedBox(height: 8),
              TextFormField(
                focusNode: _password_focus,
                controller: _textEditingController02,
                decoration: InputDecoration(
                  prefixIcon: Icon(Icons.password),
                  labelText: "密码",
                  hintText: "password",
                ),
                obscureText: true,
                textInputAction: TextInputAction.next,
              ),
              SizedBox(height: 8),
              TextFormField(
                focusNode: _parepeat_focus,
                controller: _textEditingController03,
                autovalidate: true,
                decoration: InputDecoration(
                  prefixIcon: Icon(Icons.password),
                  labelText: "再次输入",
                  hintText: "password",
                ),
                obscureText: true,
                textInputAction: TextInputAction.send,
                validator: (v) {
                  if (_textEditingController02.text !=
                      _textEditingController03.text) {
                    return "两次密码输入不一致！！";
                  }
                },
              ),
              // SizedBox(height: 10),
              // GestureDetector(
              //   child: Container(
              //     width: 420,
              //     child: Text(
              //       "No account? Register here.",
              //       textAlign: TextAlign.right,
              //       style: TextStyle(
              //         color: const Color(0xff414833),
              //       ),
              //     ),
              //   ),
              //   onTap: () {
              //     print("Go to the register page");
              //   },
              // ),
              SizedBox(height: 30),
              RaisedButton(
                onPressed: () {
                  print("username:" + _textEditingController01.text);
                  print("password:" + _textEditingController02.text);
                  print("password repeat:" + _textEditingController03.text);
                  // if((_globalKey.currentState as FormState ).validate().toString() == true){
                  //   HttpServer.signUp(_textEditingController01.text, _textEditingController02.text);}
                  // print((_globalKey.currentState as FormState)
                  //     .validate()
                  //     .toString());
                  Navigator.of(context).pop();
                },
                child: Text(
                  "注册",
                  style:
                      TextStyle(fontSize: 20, color: const Color(0xff414833)),
                ),
                color: const Color(0xffc2c5aa),
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(8),
                  side: BorderSide(
                    color: const Color(0xffa4ac86),
                    width: 2,
                  ),
                ),
              )
            ],
          ),
        ));
  }
}
